<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Service\MiniProgramService;
use Illuminate\Console\Command;

class Returns extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'return:order {sn}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $order = Order::where('order_sn',$this->argument('sn') )->firstOrFail();
        $order->total_amount;
        $res = app(MiniProgramService::class)->refund(
            $order->payment_sn,
            $order->order_sn,
            intval(bcmul($order->total_amount, 100)),
            intval(bcmul($order->total_amount, 100)),
            ['refund_desc' => '系统自动退款']
        );
        if ($res['result_code'] == 'FAIL') {
            $this->info($res['err_code_des']);
        }
        $this->info('退款成功');
    }
}
